package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductInfo.LOAN_PRODUCT_INFO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.tuanzi.server.loan_base.constant.WholeAPIEnum;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.repository.family.custom.ProductInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.ProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.family.ProductInfoSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({"rawtypes"})
public class ProductInfoRepositoryImpl extends JOOQBaseRepository implements ProductInfoRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return LOAN_PRODUCT_INFO;
    }

    @Override
    public Page<ProductInfoSearchResponse> list(ProductInfoSearchRequest searchRequest, Pageable pageable) {
    		eq(LOAN_PRODUCT_INFO.ID, searchRequest.getId());
        contains(LOAN_PRODUCT_INFO.NAME, searchRequest.getName());
        contains(LOAN_PRODUCT_INFO.STATUS, searchRequest.getStatus());
        orderBy(LOAN_PRODUCT_INFO.POSITION, Sort.Direction.ASC);
        orderBy(LOAN_PRODUCT_INFO.NAME, Sort.Direction.ASC);
        return search(pageable, new ProductInfoSearchResponseRecordMapper());
    }

    private final class ProductInfoSearchResponseRecordMapper implements
            RecordMapper<Record, ProductInfoSearchResponse> {

        @Override
        public ProductInfoSearchResponse map(Record record) {
            ProductInfoSearchResponse response = new ProductInfoSearchResponse();
            response.setId(record.get(LOAN_PRODUCT_INFO.ID));
            response.setName(record.get(LOAN_PRODUCT_INFO.NAME));
            response.setLogo(record.get(LOAN_PRODUCT_INFO.LOGO));
            response.setPosition(record.get(LOAN_PRODUCT_INFO.POSITION));
            response.setStatus(record.get(LOAN_PRODUCT_INFO.STATUS));
            response.setUpdator(record.get(LOAN_PRODUCT_INFO.UPDATOR));
            response.setUpdateTime(record.get(LOAN_PRODUCT_INFO.UPDATE_TIME));
            response.setCreateTime(record.get(LOAN_PRODUCT_INFO.CREATE_TIME));
            response.setMinlimit(record.get(LOAN_PRODUCT_INFO.MINLIMIT));
            response.setMaxlimit(record.get(LOAN_PRODUCT_INFO.MAXLIMIT));
            response.setWeight(record.get(LOAN_PRODUCT_INFO.WEIGHT));
            response.setRemark(record.get(LOAN_PRODUCT_INFO.REMARK));
            return response;
        }
    }

    @Override
    public List<ProductInfoSearchResponse> listAllUnionProductInfo() {
        neq(LOAN_PRODUCT_INFO.UNION_TYPE, 0);
        return search(new ProductInfoSearchResponseRecordMapper());
    }

    @Override
    public List<ProductInfo> listAllProductInfo() {
        List<ProductInfo> productInfos = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME).from(LOAN_PRODUCT_INFO).fetchInto(ProductInfo.class);
        return productInfos;
    }

    @Override
    public List<ProductInfo> listAllProductInfoByStatus(Integer status) {
        List<ProductInfo> productInfos = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME)
                .from(LOAN_PRODUCT_INFO).where(LOAN_PRODUCT_INFO.STATUS.eq(status)).fetchInto(ProductInfo.class);
        return productInfos;
    }

    @Override
    public List<ProductInfo> listAllProductInfoByCompanyId(Long companyId) {
        List<ProductInfo> productInfoList = dslContext.select(LOAN_PRODUCT_INFO.ID, LOAN_PRODUCT_INFO.NAME)
                .from(LOAN_PRODUCT_INFO).where(LOAN_PRODUCT_INFO.COMPANY_ID.eq(companyId))
                .fetchInto(ProductInfo.class);
        return productInfoList;
    }

    @Override
    public Page<ProductInfoSearchResponse> getApiList(ProductInfoSearchRequest searchCondition, Pageable pageable) {
        List<Integer> list = new ArrayList<>();
        WholeAPIEnum[] values = WholeAPIEnum.values();
        for (WholeAPIEnum value : values) {
            list.add(value.getId());
        }
        contains(LOAN_PRODUCT_INFO.NAME, searchCondition.getName());
        contains(LOAN_PRODUCT_INFO.STATUS, searchCondition.getStatus());
        in(LOAN_PRODUCT_INFO.API, list);
        orderBy(LOAN_PRODUCT_INFO.POSITION, Sort.Direction.ASC);
        orderBy(LOAN_PRODUCT_INFO.NAME, Sort.Direction.ASC);
        return search(pageable, new ProductInfoSearchResponseRecordMapper());
    }
}
